#!/bin/sh

if [ -e /tmp/tor.check ] ; then
	exit
fi

touch /tmp/tor.check

tor_dir=$(uci get tor.global.data_dir)
if [ -z "$tor_dir" ] ; then
	tor_dir="/var/tor"
fi
cache_file="$tor_dir/cached-consensus"
if [ ! -f "$cache_file" ] ; then
	cache_file="$tor_dir/cached-microdesc-consensus"
fi

ipset --create  tor_relays iphash >/dev/null 2>&1
old_md5sum=""
if [ -f /tmp/tor-consensus-md5 ] ; then
	old_md5sum=$(cat  /tmp/tor-consensus-md5 )
fi

if [ -f "$cache_file" ] ; then
	new_md5sum=$(md5sum "$cache_file" | awk ' { print $1 ; } ' )
	if [ "$old_md5sum" != "$new_md5sum" ] ; then 
		tor_ips=$(cat "$cache_file" | grep "^r " | awk ' { print $(NF-2) } ' | sort | uniq)
		ipset -F tor_relays
		for tip in $tor_ips ; do
			ipset --add tor_relays $tip
		done
		echo "$new_md5sum" > /tmp/tor-consensus-md5
	fi
fi


tor_enabled=$(uci get tor.global.enabled 2>/dev/null)
tor_client=$( uci get tor.client.client_mode 2>/dev/null)
tor_relay=$(  uci get tor.relay.relay_mode 2>/dev/null)


if [ $tor_enabled == "1" ] && [ "$tor_client$tor_relay" != "00" ] ; then
	found_process=$(ps | grep "tor\.pid")
	if [ -z "$found_process" ] ; then
		logger "Tor seems to have died unexpectedly, restarting"
		/etc/init.d/tor restart
	fi
fi

rm -rf /tmp/tor.check

